﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using System.Text.RegularExpressions;
using NCKH.Controllers;
using System.Xml;
using Library.UI;
using Library.UI.Form;
using Library.UI.Grid;
using NCKH.models;

namespace NCKH.View.XepLichThi.KhoiTao
{
    public partial class frmTaoHocKy : DevExpress.XtraEditors.XtraForm
    {
        public frmTaoHocKy()
        {
            InitializeComponent();
        }
        public static string namhoc { get; set; }
        public static int kyhoc { get; set; }
        public static int dot { get; set; }
        
        private void frmTaoHocKy_Load(object sender, EventArgs e)
        {
            txtNamBatDau.SetMask("[2-9][0-9]{3}");

            txtNamKetThuc.SetMask("[2-9][0-9]{3}");
            cmbHocKy.Items.Add("1A");
            cmbHocKy.Items.Add("1B");
            cmbHocKy.Items.Add("2");
            dteNgayBatDau.Config("dd/MM/yyyy", DateTime.Now);
            dteNgayKetThuc.Config("dd/MM/yyyy", DateTime.Now);
            
            
        }


        private void txtNamBatDau_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtNamBatDau.Text) || (txtNamBatDau.Text.Length != 4))
            {
                MessageBox.Show("Chưa nhập dữ liệu năm học");
                txtNamBatDau.SetFocus();
            }
            else
            {
                var value = (DateTime)dteNgayBatDau.EditValue;
                dteNgayBatDau.EditValue = new DateTime(int.Parse(txtNamBatDau.Text), value.Month, value.Day);
                var value1 = (DateTime)dteNgayKetThuc.EditValue;
                dteNgayKetThuc.EditValue = ((DateTime)dteNgayBatDau.EditValue).AddDays(30);
                txtNamKetThuc.Text = (int.Parse(txtNamBatDau.Text) + 1).ToString();
                
            }


        }

        private void txtNamKetThuc_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtNamKetThuc.Text) || (txtNamKetThuc.Text.Length!=4))
            {
                MessageBox.Show("Chưa nhập dữ liệu năm học");
                txtNamKetThuc.SetFocus();
            }
            else
                {
                    txtNamBatDau.Text = (int.Parse(txtNamKetThuc.Text) - 1).ToString();
                    var value = (DateTime)dteNgayBatDau.EditValue;
                    dteNgayBatDau.EditValue = new DateTime(int.Parse(txtNamBatDau.Text), value.Month, value.Day);
                    var value1 = (DateTime)dteNgayKetThuc.EditValue;
                    dteNgayKetThuc.EditValue = ((DateTime)dteNgayBatDau.EditValue).AddDays(30);
                    txtNamKetThuc.Text = (int.Parse(txtNamBatDau.Text) + 1).ToString();
                }
        }

        private void cmbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            
                if (cmbHocKy.Text == "1A")
                {
                    chkKyPhu.Checked = true;
                }
                else
                {
                    chkKyPhu.Checked = false;
                }

                 Context db = new Context();
                 var values = db.PLAN_HocKyDangKy_TCs.Where(a => a.Nam_hoc == string.Format("{0}-{1}", txtNamBatDau.Text, txtNamKetThuc.Text) && a.Hoc_ky == (cmbHocKy.Text == "2" ? 2 : 1) && a.Ky_phu == chkKyPhu.Checked).ToList();
                 if (values.Any())
                 {
                     spiDot.Text = (values.Max(a => a.Dot) + 1).ToString();

                 }
                 else
                 {
                     if (cmbHocKy.Text == "1A")
                     {
                    
                         spiDot.Value = 1;
                      
                     }
                     else
                         if (cmbHocKy.Text == "2")
                         {
                           
                             spiDot.Value = 1;
                             
                         }
                         
                         else
                             if (cmbHocKy.Text == "1B")
                             {
                             
                                 spiDot.Value = 2;
                                 
                             }
 
                     
                 }
                
            }

        private void btnTaoHocKy_Click(object sender, EventArgs e)
        {
            try
            {
                namhoc = string.Format("{0}-{1}", txtNamBatDau.Text, txtNamKetThuc.Text);
                kyhoc = cmbHocKy.Text == "2" ? 2 : 1;
                dot = (int)spiDot.Value;
                Context db = new Context();
                var Hocky = new PLAN_HocKyDangKy_TC
                {
                    Dot = (int)spiDot.Value,
                    Hoc_ky = cmbHocKy.Text == "2" ? 2 : 1,
                    Nam_hoc = string.Format("{0}-{1}", txtNamBatDau.Text, txtNamKetThuc.Text),
                    Tu_ngay = (DateTime)dteNgayBatDau.EditValue,
                    Den_ngay = (DateTime)dteNgayKetThuc.EditValue,
                    Chon_dang_ky = false,
                    Ky_phu = cmbHocKy.Text == "1A" ? true : false,
                    Xep_lich = false,


                };

                db.PLAN_HocKyDangKy_TCs.InsertOnSubmit(Hocky);
                db.SubmitChanges();
                MessageBox.Show("Tạo học kỳ thành công");
                frmThamSoHeThong a = new frmThamSoHeThong();
                a.ShowDialog();
                this.Close();
            }
            catch
            {
                MessageBox.Show("Lỗi");
            }
           
        }

        private void dteNgayKetThuc_EditValueChanged(object sender, EventArgs e)
        {
            //var value = (DateTime)dteNgayKetThuc.EditValue;
            
        }

        private void dteNgayBatDau_EditValueChanged(object sender, EventArgs e)
        {
            
        }

        private void dteNgayKetThuc_Validating(object sender, CancelEventArgs e)
        {
            txtNamKetThuc.Text = dteNgayKetThuc.DateTime.Year.ToString();
            txtNamBatDau.Text = (int.Parse(txtNamKetThuc.Text) - 1).ToString();
            var value = (DateTime)dteNgayKetThuc.EditValue;
            var value1 = (DateTime)dteNgayBatDau.EditValue;
            dteNgayBatDau.EditValue = new DateTime(value.Year, value1.Month, value1.Day);
        }

        private void dteNgayBatDau_Validating(object sender, CancelEventArgs e)
        {
            txtNamBatDau.Text = dteNgayBatDau.DateTime.Year.ToString();
            txtNamKetThuc.Text = (int.Parse(txtNamBatDau.Text) + 1).ToString();
            var value = (DateTime)dteNgayKetThuc.EditValue;
            var value1 = (DateTime)dteNgayBatDau.EditValue;
            dteNgayKetThuc.EditValue = new DateTime(value1.Year, value.Month, value.Day);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        }
}
